package com.example.backgroundmanger.controller;


import com.example.backgroundmanger.commons.Result;
import com.example.backgroundmanger.websocket.WebSocketServer;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * WebSocket测试控制器
 */
@RestController
@RequestMapping("/websocket")
@Tag(name = "WebSocket管理")
public class WebSocketController {
    
    /**
     * 向指定用户发送消息
     */
    @PostMapping("/sendToUser")
    @Operation(summary = "向指定用户发送消息")
    public Result<?> sendToUser(@RequestParam String userId, @RequestParam String message) {
        WebSocketServer.sendMessageToUser(userId, message);
        return Result.success("消息发送成功");
    }
    
    /**
     * 广播消息给所有在线用户
     */
    @PostMapping("/sendToAll")
    @Operation(summary = "广播消息")
    public Result<?> sendToAll(@RequestParam String message) {
        WebSocketServer.sendMessageToAll(message);
        return Result.success("广播消息发送成功");
    }
    
    /**
     * 获取在线人数
     */
    @GetMapping("/getOnlineCount")
    @Operation(summary = "获取在线人数")
    public Result<?> getOnlineCount() {
        Map<String, Object> data = new HashMap<>();
        data.put("onlineCount", WebSocketServer.getOnlineCount());
        return Result.success(data);
    }
    
    /**
     * 获取所有在线用户列表
     */
    @GetMapping("/getOnlineUsers")
    @Operation(summary = "获取在线用户列表")
    public Result<?> getOnlineUsers() {
        return Result.success(WebSocketServer.getOnlineUsers());
    }
}
